package com.jxpanda.spring.module.auth.endpoint;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Map;

@Data
@Schema(description = "OAuth2 请求参数对象，对象属性兼容snake_case风格和camelCase风格传递，根据grantType的值不同，传递不同的参数")
public class OAuth2Request {

    @Schema(description = "授权类型，默认取值如下 authorization_code、password、client_credentials、refresh_token、wechat_open_platform、wechat_mini_program", requiredMode = Schema.RequiredMode.REQUIRED, example = "password")
    @JsonAlias({"grant_type", "grantType"})
    private String grantType;

    @Schema(description = "客户端 ID，微信的appId用这个字段传递，", requiredMode = Schema.RequiredMode.REQUIRED, example = "my-client-id", defaultValue = "local")
    @JsonAlias({"client_id", "clientId"})
    private String clientId = "local";

    @Schema(description = "客户端密钥，仅适用于 client_credentials 授权类型(grantType = client_credentials)，其他类型不用传递", example = "my-client-secret")
    @JsonAlias({"client_secret", "clientSecret"})
    private String clientSecret;

    @Schema(description = "请求的权限范围", example = "read write")
    private String scope;

    @Schema(description = "授权码，适用于authorization_code、wechat_open_platform、wechat_mini_program模式(grantType = authorization_code/wechat_open_platform/wechat_mini_program)", example = "auth-code")
    private String code;

    @Schema(description = "重定向 URI，仅适用于授权码模式(grantType = authorization_code)", example = "https://example.com/callback")
    @JsonAlias({"redirect_uri", "redirectUri"})
    private String redirectUri;

    // Password Grant
    @Schema(description = "用户名，仅适用于密码模式(grantType = password)，如果邮箱/手机号就是账号，也用这个字段传递", example = "Panda")
    private String username;

    @Schema(description = "用户密码，仅适用于密码模式", example = "secure_password")
    private String password;

    // 验证码登录
    @Schema(description = "用户邮箱，仅适用于验证码登录", example = "user@example.com")
    private String email;

    @Schema(description = "用户手机号码，仅适用于验证码登录", example = "1234567890")
    private String phone;

    @Schema(description = "验证码，仅适用于验证码登录", example = "123456")
    private String captcha;

    // Refresh Token Grant
    @Schema(description = "刷新令牌，仅适用于 refresh_token 模式(grantType = refresh_token)", example = "refresh-token-value")
    @JsonAlias({"refresh_token", "refreshToken"})
    private String refreshToken;

    @Schema(description = "附加参数，用于传递自定义参数，key-value结构", example = "{\"key1\":\"value1\",\"key2\":\"value2\"}")
    private Map<String, Object> additionalParameters;

    @JsonIgnore
    public String getCredentials() {
        return GrantType.of(grantType).getCredential(this);
    }
}

